package com.example.springstack.springboot.spring5.streamDemo;

import com.example.springstack.springboot.spring5.entity.StreamDemoUser;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author qiangfanghao
 * @date
 * Stream 流各种实用用法记录
 */
@Service("steamDemoService")
public class StreamDemoServiceImpl implements StreamDemoService {

    /**
     * stream流中有两个匹配的接口，anyMatch和allMatch；
     * anyMatch: 只要有一条数据可以匹配，那就会返回true
     * allMatch： 必须要每一条数据都匹配才会返回true
     *
     * 可以配合 filter 使用
     */

    /**
     * stream流排序
     * @param streamDemoUsers
     */
    @Override
    public void streamSort(List<StreamDemoUser> streamDemoUsers) {
        //对 id进行排序 升序
        List<StreamDemoUser> ascCollect = streamDemoUsers
                .stream()
                .sorted(Comparator.comparing(StreamDemoUser::getId))
                .collect(Collectors.toList());
        System.out.println("正序集合-->"+ascCollect);

        //对 id进行排序 降序
        List<StreamDemoUser> descCollect = streamDemoUsers
                .stream()
                .sorted(Comparator.comparing(StreamDemoUser::getId).reversed())
                .collect(Collectors.toList());
        System.out.println("倒叙集合-->"+descCollect);
    }

    /**
     * 去重
     * @param streamDemoUsers
     */
    @Override
    public void streamDistinct(List<StreamDemoUser> streamDemoUsers) {

        List<StreamDemoUser> distinctCollect = streamDemoUsers
                .stream()
                .distinct()
                .collect(Collectors.toList());

        System.out.println("去重集合-->"+distinctCollect);
    }

    /**
     * Collectors  应用
     * @param streamDemoUsers
     */
    @Override
    public void streamCollectors(List<StreamDemoUser> streamDemoUsers) {

        /**
         * 注意: groupingBy 还有两个重载方法,分布:
         *  1,增加了对流中元素的处理方式的Collector，比如上面的默认的处理方法就是Collectors.toList()
         *  2,添加了结果Map的生成方法。
         *
         * 以及并发版的groupingByConcurrent
         */
        //根据字段绩进行分组过滤
        Map<String, List<StreamDemoUser>> groupingByCollect = streamDemoUsers
                .stream()
                .collect(Collectors.groupingBy(StreamDemoUser::getUsername));
        System.out.println("根据username字段进行分组过滤-->"+groupingByCollect);

        //拼接字符串
        List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
        // 指定连接符和前后缀
        String resultStr = list
                .stream()
                .collect(Collectors.joining("-","",""));
        System.out.println("Collectors joining 拼接字符串"+resultStr);

        /**
         * 该方法将流中的元素按照给定的校验规则的结果分为两个部分，放到一个map中返回，
         * map的键是Boolean类型，
         * 值为元素的列表List。
         */
        Map<Boolean,List<String>> partitioningByMap = list
                .stream()
                .collect(Collectors.partitioningBy(e -> e.length()>5));
        Map<Boolean, Set<String>> partitioningByMap2 = list
                .stream()
                .collect(Collectors.partitioningBy(e -> e.length()>6,Collectors.toSet()));
        System.out.println("根据条件过滤-->"+partitioningByMap.toString() + "\n" + partitioningByMap2.toString());

    }

    /**
     * 获取字符串中指定字符的索引
     * @param testStr
     * @param flag
     */
    @Override
    public void intStreamIndex(String testStr, char flag) {
        List<Integer> collect = IntStream.range(0, testStr.length())
                //过滤获取到索引坐标
                .filter(index -> testStr.charAt(index) == flag).boxed()
                .collect(Collectors.toList());

        System.out.println("索引下标数组->"+collect.toString());
    }


}
